package game.module.nn.processor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import game.module.nn.bean.NnRound;
import game.module.nn.logic.NnDataManager;
import game.session.SessionManager;
import lion.common.MsgCodeAnn;
import lion.math.RandomDispatcher;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageLogin.S2ErrorCode;
import lua.LuaMessageNn.C2SNnXiaZhu;
import lua.LuaMessageNn.PushNnFaPai;
import lua.LuaMessageNn.PushNnXiaZhu;
import lua.LuaMessageNn.S2CNnXiaZhu;

@MsgCodeAnn(msgcode = C2SNnXiaZhu.id, accessLimit = 500)
public class NnXiaZhuProcessor extends PlayingRoleMsgProcessor {

	private static Logger logger = LoggerFactory.getLogger(NnXiaZhuProcessor.class);

	@Override
	public void process(PlayingRole playingRole, RequestMessageRaw request) throws Exception {
		C2SNnXiaZhu reqMsg = C2SNnXiaZhu.parse(request);
		short zhu = reqMsg.zhu;
		int playerId = playingRole.getId();
		logger.info("nn xia zhu,playerId={},zhu={}", playerId, zhu);
		// 房间是否存在
		NnRound ddzRound = NnDataManager.getInstance().getNnRoom(playerId);
		if (ddzRound == null) {
			S2ErrorCode respMsg = new S2ErrorCode(201);
			playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
			return;
		}
		// 是否已经产生地主
		if (ddzRound.getZhuangJiaIndex() == null) {
			S2ErrorCode respMsg = new S2ErrorCode(305);
			playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
			return;
		}
		// 是否我是庄家
		int myIndex = ddzRound.getPlayerIds().indexOf(playerId);
		if (myIndex == ddzRound.getZhuangJiaIndex()) {
			S2ErrorCode respMsg = new S2ErrorCode(306);
			playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
			return;
		}
		// 是否已经下注
		Map<Integer, Short> xiaZhuMap = ddzRound.getXianJiaXiaZhu();
		if (xiaZhuMap != null && xiaZhuMap.get(myIndex) != null) {
			S2ErrorCode respMsg = new S2ErrorCode(307);
			playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
			return;
		}
		// 保存
		if (xiaZhuMap == null) {
			xiaZhuMap = new HashMap<>();
			ddzRound.setXianJiaXiaZhu(xiaZhuMap);
		}
		xiaZhuMap.put(myIndex, zhu);
		// ret
		S2CNnXiaZhu respMsg = new S2CNnXiaZhu();
		playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));
		// push
		PushNnXiaZhu pushMsg = new PushNnXiaZhu((short) myIndex, zhu);
		List<Integer> playerIds = ddzRound.getPlayerIds();
		for (Integer aPlayerId : playerIds) {
			PlayingRole pr = SessionManager.getInstance().getPlayer(aPlayerId);
			if (pr != null && pr.isChannelActive()) {
				pr.writeAndFlush(pushMsg.build(pr.alloc()));
			}
		}
		// 是否发牌
		if (xiaZhuMap.size() >= ddzRound.getPlayerIds().size() - 1) {
			faPai(ddzRound);
		}
	}

	private void faPai(NnRound ddzRound) {
		RandomDispatcher<Byte> faPaiTool = ddzRound.getFaPaiTool();
		List<Integer> playerIds = ddzRound.getPlayerIds();
		int playerIndex = 0;
		PushNnFaPai pushMsg = new PushNnFaPai();
		for (Integer aPlayerId : playerIds) {
			Byte randPai = faPaiTool.randomRemove();
			ddzRound.getCards().get(playerIndex++).add(randPai);
			// 推送
			pushMsg.pai = randPai;
			PlayingRole pr = SessionManager.getInstance().getPlayer(aPlayerId);
			if (pr != null && pr.isChannelActive()) {
				pr.writeAndFlush(pushMsg.build(pr.alloc()));
			}
		}
	}

	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {
		// TODO Auto-generated method stub

	}

}
